- name: Include vars
  include_vars: dir=/srv/web/infra/ansible/vars/all/ ignore_files=README
  tags:
  - s3-mirror

- name: Create s3-mirror group
  group: gid=442 name=s3-mirror
  tags:
  - s3-mirror

- name: Create s3-mirror user
  user: group=s3-mirror name=s3-mirror comment="s3-mirror user" home=/home/s3-mirror shell=/bin/bash
  tags:
  - s3-mirror

- name: Create needed directories
  file: path={{item}} owner=s3-mirror group=s3-mirror state=directory mode=0755
  with_items:
  - /var/log/s3-mirror
  - /var/lib/s3-mirror
  - /var/run/s3-mirror
  - /home/s3-mirror/.aws
  #- /var/log/s3-mirror-logs
  #- /var/log/s3-mirror-logs/bucket
  tags:
  - s3-mirror

- name: aws credentials file
  template: src=credentials dest=/home/s3-mirror/.aws/credentials owner=s3-mirror group=s3-mirror mode=0600
  tags:
  - s3-mirror

- name: aws config file
  copy: src=config dest=/home/s3-mirror/.aws/config owner=s3-mirror group=s3-mirror mode=0600
  tags:
  - s3-mirror

- name: Install packages
  package: name={{ item }} state=present
  with_items:
  - awscli
  #  - mirrormanager2-client
  tags:
  - s3-mirror

- name: s3 script
  copy: src={{item}} dest=/usr/local/bin/{{item}} owner=s3-mirror group=s3-mirror mode=0755
  with_items:
  - s3.sh
  - s3-sync-path.sh
  tags:
  - s3-mirror

- name: s3sync cron main that takes forever
  cron: name="s3sync-main" minute="0" hour="0" user="s3-mirror"
        job='/usr/local/bin/lock-wrapper s3sync-main /usr/local/bin/s3.sh 2>&1 | /usr/local/bin/nag-once s3.sh 1d 2>&1'
        cron_file=s3.sh
        disabled=true
  when: env != 'staging' and inventory_hostname.startswith('mm-backend01.')
  tags:
  - s3-mirror

- name: s3sync cron - test releases
  cron: name="s3sync-updates-current" minute="40" hour="5,9,13,19" user="s3-mirror"
        job='/usr/local/bin/lock-wrapper s3sync-test-releases "/usr/local/bin/s3-sync-path.sh /pub/fedora/linux/releases/test/" 2>&1 | /usr/local/bin/nag-once s3-test-releases.sh 1d 2>&1'
        cron_file=s3-test-releases.sh
        disabled=true
  when: env != 'staging' and inventory_hostname.startswith('mm-backend01.')
  tags:
  - s3-mirror

- name: s3sync cron - updates for current
  cron: name="s3sync-updates-current" minute="0" hour="3,9,15,21" user="s3-mirror"
        job='/usr/local/bin/lock-wrapper s3sync-updates-current "/usr/local/bin/s3-sync-path.sh /pub/fedora/linux/updates/{{ FedoraCycleNumber|int }}/Everything/x86_64/" 2>&1 | /usr/local/bin/nag-once s3-updates-current.sh 1d 2>&1'
        cron_file=s3-updates-current.sh
        disabled=true
  when: env != 'staging' and inventory_hostname.startswith('mm-backend01.')
  tags:
  - s3-mirror

- name: s3sync cron - updates for development/current+1 x86_64
  cron: name="s3sync-updates-current" minute="0" hour="2,7,10" user="s3-mirror"
        job='/usr/local/bin/lock-wrapper s3sync-updates-dev-cur-plus-1-x86_64 "/usr/local/bin/s3-sync-path.sh /pub/fedora/linux/development/{{ FedoraCycleNumber|int + 1 }}/Everything/x86_64/os/" 2>&1 | /usr/local/bin/nag-once s3-updates-dev-cur-plus-1-x86_64.sh 1d 2>&1'
        cron_file=s3-updates-dev-cur-plus-1-x86_64.sh
        disabled=true
  when: env != 'staging' and inventory_hostname.startswith('mm-backend01.')
  tags:
  - s3-mirror

- name: s3sync cron - updates for development/current+1 aarch64
  cron: name="s3sync-updates-current" minute="0" hour="4,11,18" user="s3-mirror"
        job='/usr/local/bin/lock-wrapper s3sync-updates-dev-cur-plus-1-aarch64 "/usr/local/bin/s3-sync-path.sh /pub/fedora/linux/development/{{ FedoraCycleNumber|int + 1 }}/Everything/aarch64/os/" 2>&1 | /usr/local/bin/nag-once s3-updates-dev-cur-plus-1-aarch64.sh 1d 2>&1'
        cron_file=s3-updates-dev-cur-plus-1-aarch64.sh
        disabled=true
  when: env != 'staging' and inventory_hostname.startswith('mm-backend01.')
  tags:
  - s3-mirror

- name: s3sync cron - updates for current-1
  cron: name="s3sync-updates-previous" minute="30" hour="0,6,12,18" user="s3-mirror"
        job='/usr/local/bin/lock-wrapper s3sync-updates-previous "/usr/local/bin/s3-sync-path.sh /pub/fedora/linux/updates/{{ FedoraCycleNumber|int - 1 }}/Everything/x86_64/" 2>&1 | /usr/local/bin/nag-once s3-updates-previous.sh 1d 2>&1'
        cron_file=s3-updates-previous.sh
        disabled=true
  when: env != 'staging' and inventory_hostname.startswith('mm-backend01.')
  tags:
  - s3-mirror

- name: s3sync cron - epel 7 x86_64
  cron: name="s3sync-epel7-x86_64" minute="10" hour="2,5,8,11,14,17,20,23" user="s3-mirror"
        job='/usr/local/bin/lock-wrapper s3sync-epel7-x86_64 "/usr/local/bin/s3-sync-path.sh /pub/epel/7/x86_64/" 2>&1 | /usr/local/bin/nag-once s3-epel7-x86_64.sh 1d 2>&1'
        cron_file=s3-epel7-x86_64.sh
        disabled=true
  when: env != 'staging' and inventory_hostname.startswith('mm-backend01.')
  tags:
  - s3-mirror

- name: s3sync cron - epel 7 aarch64
  cron: name="s3sync-epel7-aarch64" minute="20" hour="4,7,10,13,16,19,22" user="s3-mirror"
        job='/usr/local/bin/lock-wrapper s3sync-epel7-aarch64 "/usr/local/bin/s3-sync-path.sh /pub/epel/7/aarch64/" 2>&1 | /usr/local/bin/nag-once s3-epel7-aarch64.sh 1d 2>&1'
        cron_file=s3-epel7-aarch64.sh
        disabled=true
  when: env != 'staging' and inventory_hostname.startswith('mm-backend01.')
  tags:
  - s3-mirror

- name: s3sync cron - epel 8 Everything x86_64
  cron: name="s3sync-epel8-everything-x86_64" minute="43" hour="3,6,9,12,15,17,20,23" user="s3-mirror"
        job='/usr/local/bin/lock-wrapper s3sync-epel8-everything-x86_64 "/usr/local/bin/s3-sync-path.sh /pub/epel/8/Everything/x86_64/" 2>&1 | /usr/local/bin/nag-once s3-epel8-everything-x86_64.sh 1d 2>&1'
        cron_file=s3-epel8-everything-x86_64.sh
        disabled=true
  when: env != 'staging' and inventory_hostname.startswith('mm-backend01.')
  tags:
  - s3-mirror

- name: s3sync cron - epel 8 Everything aarch64
  cron: name="s3sync-epel8-everything-aarch64" minute="38" hour="4,7,10,13,16,19,22" user="s3-mirror"
        job='/usr/local/bin/lock-wrapper s3sync-epel8-everything-aarch64 "/usr/local/bin/s3-sync-path.sh /pub/epel/8/Everything/aarch64/" 2>&1 | /usr/local/bin/nag-once s3-epel8-everything-aarch64.sh 1d 2>&1'
        cron_file=s3-epel8-everything-aarch64.sh
        disabled=true
  when: env != 'staging' and inventory_hostname.startswith('mm-backend01.')
  tags:
  - s3-mirror

- name: s3sync cron - epel 8 Modular x86_64
  cron: name="s3sync-epel8-modular-x86_64" minute="32" hour="3,6,9,12,15,17,20,23" user="s3-mirror"
        job='/usr/local/bin/lock-wrapper s3sync-epel8-modular-x86_64 "/usr/local/bin/s3-sync-path.sh /pub/epel/8/Modular/x86_64/" 2>&1 | /usr/local/bin/nag-once s3-epel8-modular-x86_64.sh 1d 2>&1'
        cron_file=s3-epel8-modular-x86_64.sh
        disabled=true
  when: env != 'staging' and inventory_hostname.startswith('mm-backend01.')
  tags:
  - s3-mirror

- name: s3sync cron - epel 8 Modular aarch64
  cron: name="s3sync-epel8-modular-aarch64" minute="27" hour="4,7,10,13,16,19,22" user="s3-mirror"
        job='/usr/local/bin/lock-wrapper s3sync-epel8-modular-aarch64 "/usr/local/bin/s3-sync-path.sh /pub/epel/8/Modular/aarch64/" 2>&1 | /usr/local/bin/nag-once s3-epel8-modular-aarch64.sh 1d 2>&1'
        cron_file=s3-epel8-modular-aarch64.sh
        disabled=true
  when: env != 'staging' and inventory_hostname.startswith('mm-backend01.')
  tags:
  - s3-mirror

#- name: s3sync-logs cron
#  cron: name="s3sync-logs" hour="0" user="root"
#        job='/usr/local/bin/lock-wrapper s3sync-logs "/bin/sleep $((${RANDOM} \% 300)); /usr/local/bin/s3sync-logs >> /var/log/s3-mirror-logs/s3sync-logs.log 2>&1" | /usr/local/bin/nag-once s3mirror-logs 12h 2>&1'
#        cron_file=s3sync-logs
#        state=absent
